package com.mrx.commons.aop.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.sql.Connection;

/**
 * Connection 的切点, 后期可以考虑做成分布式事务
 *
 * @author Mr.X
 * @since 2023-11-15 15:49
 **/
@Aspect
@Component
public class TransactionalAop {

    private static final Logger logger = LoggerFactory.getLogger(TransactionalAop.class);

    @Pointcut("execution(* javax.sql.DataSource.getConnection(..))")
    public void transactionalPoint() {
    }

    @Around("transactionalPoint()")
    public Connection around(ProceedingJoinPoint point) throws Throwable {
        Connection connection = (Connection) point.proceed();
        logger.debug("get Connection: {}", connection);
        return connection;
    }

}
